package com.xy.zookeeper.curator;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessLock;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.framework.recipes.locks.InterProcessReadWriteLock;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class CuratorLock {
    CuratorFramework client;
    private String zkConnUrl = "127.0.0.1:2181";

    @Before
    public void before() {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        client = CuratorFrameworkFactory.builder()
                .connectString(zkConnUrl)
                .sessionTimeoutMs(5000)
                .retryPolicy(retryPolicy)
                //.retryPolicy(new RetryOneTime(3000))
                .namespace("create")
                .build();
        client.start();
    }

    @After
    public void after() {
        client.close();
    }

    @Test
    public void lock1() {
        //排它锁
        try {
            InterProcessLock interProcessLock = new InterProcessMutex(client, "/lock1");
            System.out.println("等待获取锁对象");
            interProcessLock.acquire();
            for (int i = 0; i < 10; i++) {
                System.out.println(i);
                Thread.sleep(3000);
            }
            interProcessLock.release();
            System.out.println("释放锁");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void readLock() {
        try {
            InterProcessReadWriteLock interProcessReadWriteLock = new InterProcessReadWriteLock(client, "/lock1");
            InterProcessLock interProcessLock = interProcessReadWriteLock.readLock();
            System.out.println("等待获取锁对象");
            interProcessLock.acquire();
            for (int i = 0; i < 10; i++) {
                System.out.println(i);
                Thread.sleep(3000);
            }
            interProcessLock.release();
            System.out.println("释放锁");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void writeLock() {
        try {
            InterProcessReadWriteLock interProcessReadWriteLock = new InterProcessReadWriteLock(client, "/lock1");
            InterProcessLock interProcessLock = interProcessReadWriteLock.writeLock();
            System.out.println("等待获取锁对象");
            interProcessLock.acquire();
            for (int i = 0; i < 10; i++) {
                System.out.println(i);
                Thread.sleep(3000);
            }
            interProcessLock.release();
            System.out.println("释放锁");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
